package service.cfg;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.context.ContextLoader;

import javax.servlet.ServletContext;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by yangqy on 02/01/2017.
 */
@Service
public class ResourceApiFrameworkCfg {

    @Value("#{config['resourceFramework.materialHttpRootUrl']}")
    private String materialHttpRootUrl;
    @Value("#{config['resourceFramework.materialHttpStaticRootUrl']}")
    private String materialHttpStaticRootUrl;
    @Value("#{config['resourceFramework.materialFileRootPath']}")
    private String materialFileRootPath;
    @Value("#{config['resourceFramework.watermarkImagePath2']}")
    private String materialFileRootPath2;
    @Value("#{config['resourceFramework.materialBufferSize\']}")
    private int materialBufferSize;
    @Value("#{config['resourceFramework.watermarkImagePath']}")
    private String watermarkImagePath;
    @Value("#{config['resourceFramework.nginxStaticUrl']}")
    private String nginxStaticImgUrl;
    @Value("#{config['goodsPosterMode']}")
    private String goodsPosterMode;
    private Set materialUploadImgType;
    private Set materialUploadFileType;
    private Set materialUploadExcelType;

    public ResourceApiFrameworkCfg() {
    }

    public String getGoodsPosterMode() {
        return goodsPosterMode;
    }

    public void setGoodsPosterMode(String goodsPosterMode) {
        this.goodsPosterMode = goodsPosterMode;
    }

    public String getMaterialFileRootPath2() {
        return materialFileRootPath2;
    }

    public void setMaterialFileRootPath2(String materialFileRootPath2) {
        this.materialFileRootPath2 = materialFileRootPath2;
    }

    public String getNginxStaticImgUrl() {
        return nginxStaticImgUrl;
    }

    public void setNginxStaticImgUrl(String nginxStaticImgUrl) {
        this.nginxStaticImgUrl = nginxStaticImgUrl;
    }

    public String getMaterialHttpRootUrl() {
        return StringUtils.isBlank(this.materialHttpRootUrl)?"":this.materialHttpRootUrl;
    }

    public String getMaterialHttpStaticRootUrl() {
        return StringUtils.isBlank(this.materialHttpStaticRootUrl)?"/upload":this.materialHttpStaticRootUrl;
    }

    public String getMaterialFileRootPath() {
        if(StringUtils.isBlank(this.materialFileRootPath)) {
            synchronized(this) {
                if(StringUtils.isBlank(this.materialFileRootPath)) {
                    ServletContext servletContext = ContextLoader.getCurrentWebApplicationContext().getServletContext();
                    this.materialFileRootPath = servletContext.getRealPath("/") + "/upload";
                }
            }
        }

        return this.materialFileRootPath;
    }

    public int getMaterialBufferSize() {
        return this.materialBufferSize;
    }

    public Set getMaterialUploadImgType() {
        return this.materialUploadImgType;
    }

    @Value("#{config['resourceFramework.materialUploadImgType']}")
    public void setMaterialUploadImgType(String materialUploadImgType) {
        this.materialUploadImgType = this.stringToSet(materialUploadImgType);
    }

    public Set getMaterialUploadFileType() {
        return this.materialUploadFileType;
    }

    @Value("#{config['resourceFramework.materialUploadFileType']}")
    public void setMaterialUploadFileType(String materialUploadFileType) {
        this.materialUploadFileType = this.stringToSet(materialUploadFileType);
    }

    public Set getMaterialUploadExcelType() {
        return materialUploadExcelType;
    }

    @Value("#{config['resourceFramework.materialUploadExcelType']}")
    public void setMaterialUploadExcelType(String materialUploadExcelType) {
        this.materialUploadExcelType = this.stringToSet(materialUploadExcelType);
    }

    public String getWatermarkImagePath() {
        return this.watermarkImagePath;
    }

    public Set stringToSet(String str) {
        HashSet set = new HashSet();
        if(StringUtils.isBlank(str)) {
            return set;
        } else {
            String[] arr$ = str.split(",");
            int len$ = arr$.length;

            for(int i$ = 0; i$ < len$; ++i$) {
                String d = arr$[i$];
                set.add(d);
            }

            return set;
        }
    }

}
